Learn R Programming

seem (version 1.0)

Random number gen: Custom random number generators for didactic purposes

Description

Random generator of uniform RV between a and b, exponential, and standard normal RV

Usage

ran.unif(a,b)
ran.exp(rate)
ran.norm()

Arguments

a
lower value
b
upper value
rate
rate parameter of exponential RV

Value

  • One value of a RV according to distribution selected

Details

For didactic purposes, we show how to build custom random number generators from the basic uniform distribution between 0 and 1. The following function defines a uniform generator between a and b using the scaling and shifting of U(0,1).

References

Acevedo M.F. 2012. Simulation of Ecological and Environmental Models. CRC Press.

See Also

Random number generators runif, rexp, rnorm

Examples

Run this code
yunif <- array(); a <- 5;b <- 15
for(i in 1:10) yunif[i] <- ran.unif(a,b)
hist(yunif,prob=TRUE); title(sub="Uniform in 5,15") 

yexp <- array(); rate <- 1/10
for(i in 1:10) yexp[i] <- ran.exp(rate)
hist(yexp,prob=TRUE); title(sub="Exponential of rate 0.1") 

ynorm <- array(); 
for(i in 1:10) ynorm[i] <- ran.norm()
hist(ynorm,prob=TRUE); title(sub="Std Normal")

Run the code above in your browser using DataLab